import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import localCache from '@/utils/cache'
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/main'
    },
    {
      path: '/login',
      name: 'about',
      component: () => import('../views/login/login.vue')
    },
    {
      path: '/main',
      name: 'main',
      component: () => import('../views/main/main.vue')
      // children: [
      //   {
      //     path: '/main/system/user/role',
      //     component: () => import('@/views/main/system/user/role.vue')
      //   }
      // ]
    },
    {
      path: '/:pathMatch(.*)*',
      component: () => import('../views/not-found/not-found.vue')
    }
  ]
})

router.beforeEach((to) => {
  console.log(444, to.path)
  if (to.path !== '/login') {
    // 如果访问的不是登录页，需要判断是否已登录
    const token = localCache.getCache('token')
    if (!token) return '/login'
  }
})

export default router
